#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
plots Binder cumulant
'''

from __future__ import division

import matplotlib.pyplot
import sys

import common
import os
import math

path = os.path.join('..', '..', 'results' )

modelName = common.choice(path)

J = common.choice(os.path.join(path, modelName))
B = common.choice(os.path.join(path, modelName, J))
L = common.choice(os.path.join(path, modelName, J, B))

if type(L) == str:
    L = [L]

for tL in L:
    lPath = os.path.join(path, modelName, J, B, tL)
    M2 = []
    for T in os.listdir(os.path.join(lPath, 'OPT2')):
        try:
            variable = eval( open( os.path.join(lPath, 'OPT2', T)  ).read() )
        except:
            pass
        else:
            M2 += [ ( float(T.replace('T','')), variable ) ]
    
    M2.sort()
    
    t2 = [ tx[0] for tx in M2 ]
    m2 = [ tx[1] for tx in M2 ]
    
    M4 = []
    for T in os.listdir(os.path.join(lPath, 'OPT4')):
        try:
            variable = eval( open( os.path.join(lPath, 'OPT4', T)  ).read() )
        except:
            pass
        else:
            M4 += [ ( float(T.replace('T','')), variable ) ]
    
    M4.sort()
    
    t4 = [ tx[0] for tx in M4 ]
    m4 = [ tx[1] for tx in M4 ]
    
    result = []
    for i in xrange(len(m4)):
        result += [ 1 - m4[i] / (3 * m2[i]**2 ) ]
        
    matplotlib.pyplot.plot(t2, result, label = tL)
    
matplotlib.pyplot.legend()
matplotlib.pyplot.show()